import 'dart:collection';
import 'dart:convert';

import 'package:get/get.dart';
import 'package:points24/ext_widget/double_util.dart';

class GameExpress {

  double n1;
  double n2;
  String execute = "";//执行的计算方式 加减乘除
  double result = 0;


  GameExpress(this.n1, this.n2, this.execute, this.result);

  GameExpress clone(){
    return GameExpress.create(n1, n2, execute, result);
  }


  toMap(){

    return Map.of({
      "n1": n1,
      "n2": n2,
      "execute": execute,
      "result": result,
    });
  }

  String toJson() {

    return json.encode({
      "n1": n1,
      "n2": n2,
      "execute": execute,
      "result": result,
    });

  }


  bool isReach(double targetNo) {
    return result == targetNo;
  }


  double computeRealValue() {
    double v = 0;

    switch (execute) {
      case Excute.plus:
        v=n1+n2;
        break;
      case Excute.sub:
        v=n1-n2;
        break;
      case Excute.mul:
        v=n1*n2;
        break;
      case Excute.div:
        v=n1/n2;
        break;
    }

    return v;
  }

  String bestResult() {
    if (result == result.toInt()) {
      return result.toInt().toString();
    } else {
      //只有除法才会走到这步，其他的步骤结果都是整数
      String niceN1 = (n1 == n1.toInt()) ? n1.toInt().toString() : n1.toString();
      String niceN2 = (n2 == n2.toInt()) ? n2.toInt().toString() : n2.toString();
      return "${niceN1}/${niceN2}";
    }

    // return result.toString();
    // String niceResult = result.toString();
    // return niceResult;
    //
  }

  GameExpress.create(this.n1, this.n2, this.execute, this.result);

  static List<GameExpress> createAll(double n1, double n2) {
    var list = <GameExpress>[];

    Set<double> resultSets = HashSet();

    //+
    double addV = (n1 + n2).toDouble();
    if (!resultSets.contains(addV)) {
      list.add(GameExpress.create(n1, n2, Excute.plus, addV));
      resultSets.add(addV);
    }

    //-
    double subV1 = (n1 - n2).toDouble();
    if (!resultSets.contains(subV1) && subV1>0) {//排除负数
      list.add(GameExpress.create(n1, n2, Excute.sub, subV1));
      resultSets.add(subV1);
    }

    double subV2 = (n2 - n1).toDouble();
    if (!resultSets.contains(subV2)&& subV2>0) {//排除负数
      list.add(GameExpress.create(n2, n1, Excute.sub, subV2));
      resultSets.add(subV2);
    }

    //x
    double mulV = (n1 * n2).toDouble();
    if (!resultSets.contains(mulV)) {
      list.add(GameExpress.create(n1, n2, Excute.mul, mulV));
      resultSets.add(mulV);
    }

    //÷

    if (n2 != 0) {
      double divV1 = (n1 / n2).toDouble();
      if (!resultSets.contains(divV1) && divV1.isInt()) {
        list.add(GameExpress.create(n1, n2, Excute.div, divV1));
        resultSets.add(divV1);
      }
    }

    if (n1 != 0) {
      double divV2 = (n2 / n1).toDouble();
      if (!resultSets.contains(divV2) && divV2.isInt()) {
        list.add(GameExpress.create(n2, n1, Excute.div, divV2));
      }
    }

    return list;
  }

  @override
  String toString() {
    return toJson();
  }


  // @override
  // String toString() {
  //   return "===> ${n1.toInt()} ${execute} ${n2.toInt()} = ${result.toInt()}";
  // }



  String niceExpress() {
    return "${n1.toInt()} ${execute} ${n2.toInt()} = ${result.toInt()}";
  }

}

class Excute {
  static const String plus = "+";
  static const String sub = "-";
  static const String mul = "x";
  static const String div = "÷";
}
